﻿using AntMovement.Core;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovernent.SolidTechESerialCard.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovernent.SolidTechESerialCard
{
    public class Motion : IMotionBase
    {
        private int _index;
        public int Index => _index;
        private string _name;

        public string Name => _name;
        private List<Axis> _axes;
        public Motion(string name, int index, List<Axis> axes)
        {
            _name = name;
            _index = index;
            _axes = axes;
        }

        public string Close()
        {
            SLDMV.SLDM_Close(0);
            return "ok";
        }

        public long GetStatus()
        {
            int status = 0;
            int error = SLDMV.SLDM_Get_ConnStatus(0, ref status);
            if (error != 0)
            {
                return -1;
            }
            return status;
        }

        public string Open()
        {
            UInt32[] Card_Type = new UInt32[20];
            UInt32[] Get_Card_Type = new UInt32[20];
            uint nCardNo = 0;
            SLDMV.SLDM_Get_Card_List(Convert.ToUInt32(_name), ref nCardNo, Get_Card_Type);
            for (ulong i = 0; i < nCardNo; i++)
            {
                Card_Type[i] = Get_Card_Type[i];
            }
            SLDMV.SLDM_Set_Card_List(Convert.ToUInt32(_name), nCardNo, Card_Type);
            long error = SLDMV.SLDM_Open(Convert.ToUInt32(_name), 0);
            if (error == 0)
            {
                //SLDMV.SLDM_Clear_Alarm_History(nCardNo);
                #region 轴参数初始化
                foreach (var axis in _axes)
                {
                    if (axis.Parameter == null)
                    {
                        continue;
                    }
                    error = SLDMV.SLDM_Set_Pulse_Mode(axis.CardNo, axis.AxisID, (uint)axis.Parameter.OutMode);
                    if (error != 0)
                    {
                        return($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Pulse_Mode[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Pulse_Pol(axis.CardNo, axis.AxisID, (uint)(axis.Parameter.Pulse_Pol ? 0 : 1));
                    if (error != 0)
                    {
                        return($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Pulse_Pol[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Dir_Pol(axis.CardNo, axis.AxisID, (uint)(axis.Parameter.Dir_Pol ? 0 : 1));
                    if (error != 0)
                    {
                        return($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Dir_Pol[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Enc_Pol(axis.CardNo, axis.AxisID, (uint)axis.Parameter.Enc_Pol);
                    if (error != 0)
                    {
                        return($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Enc_Pol[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Enc_Mult(axis.CardNo, axis.AxisID, 4);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Enc_Mult[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_EncZ_Pol(axis.CardNo, axis.AxisID, (uint)axis.Parameter.EncZ_Pol);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_EncZ_Pol[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Plimit_Config(axis.CardNo, axis.AxisID, (uint)(axis.Parameter.Enable_Plimit ? 1 : 0), (uint)axis.Parameter.Plimit_Pol);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Plimit_Config[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Nlimit_Config(axis.CardNo, axis.AxisID, (uint)(axis.Parameter.Enable_Nlimit ? 1 : 0), (uint)axis.Parameter.Nlimit_Pol);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Nlimit_Config[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Org_Config(axis.CardNo, axis.AxisID, 1, (uint)axis.Parameter.Polarity);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Org_Config[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_ServoAlm_Config(axis.CardNo, axis.AxisID, (uint)(axis.Parameter.Enable_AxisAlarm ? 1 : 0), 0);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_ServoAlm_Config[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_RstAlm(axis.CardNo, axis.AxisID, 1);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_RstAlm[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_ServoOn(axis.CardNo, axis.AxisID, 1);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_ServoOn[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Cpos(axis.CardNo, axis.AxisID, 0);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Cpos[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Set_Enc(axis.CardNo, axis.AxisID, 0);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Set_Enc[{(SolidErrorCode)error}]");
                    }
                    error = SLDMV.SLDM_Reset(axis.CardNo, axis.AxisID);
                    if (error != 0)
                    {
                        return ($"CardNo[{axis.CardNo}] Index[{axis.AxisID}] Solid Tech E ==>> SLDM_Reset[{(SolidErrorCode)error}]");
                    }
                }
                #endregion
                return "ok";
            }
            return $"SLDM_Open ==>> {(SolidErrorCode)error}";
        }
    }
}
